//
//  YPRedPacketTableViewCell.m
//  ChengHuiTong
//
//  Created by ypwl on 16/8/5.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPRedPacketTableViewCell.h"
#import "NSDate+Extension.h"
#import "NSString+Extension.h"
@implementation YPRedPacketTableViewCell{
    
    UIButton*_backGroundButton;
    
    UILabel*_moneyLabel,*_moneyTitle,*_conditionTiele,*_time,*_packetStyle;
    
}

- (void)awakeFromNib {
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        _backGroundButton=[UIButton buttonWithType:UIButtonTypeCustom];
        
        _backGroundButton.frame=CGRectMake(10, 15, SCREEN_WIDTH-20,(SCREEN_WIDTH-20)*0.4);
        
       
        
        [self.contentView addSubview:_backGroundButton];
        
        UILabel *title= [[UILabel alloc] initWithFrame:CGRectMake(15, _backGroundButton.height*0.23, 40, 12)];
        
        title.text=@"优惠券";
        
        title.textColor = [UIColor whiteColor];
        
        title.font = [UIFont systemFontOfSize:12];
        
        [_backGroundButton addSubview:title];
        
        _moneyLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, title.bottom+5, _backGroundButton.width/3.0-15, 40)];
        
        _moneyLabel.font=[UIFont systemFontOfSize:11];
        
        _moneyLabel.textColor=[UIColor whiteColor];
        
        [_backGroundButton addSubview:_moneyLabel];
        
        
        _packetStyle = [[UILabel alloc] initWithFrame:CGRectMake(title.left, _moneyLabel.bottom+10, _moneyLabel.width, 13)];
        
        _packetStyle.textColor = [UIColor whiteColor];
        
        _packetStyle.font = [UIFont systemFontOfSize:12];
        
        [_backGroundButton addSubview:_packetStyle];

        

        UILabel*explainLable=[[UILabel alloc] initWithFrame:CGRectMake(_backGroundButton.width/3.0+15, title.top, _backGroundButton.width-(_backGroundButton.width/3.0+15)-10, 12)];
        
        explainLable.textColor=[UIColor whiteColor];
        
        explainLable.font=[UIFont systemFontOfSize:11];
        
        explainLable.text=@"使用说明:";
        
        [_backGroundButton addSubview:explainLable];
        
//        _moneyTitle = [[UILabel alloc] initWithFrame:CGRectMake(explainLable.left+10, explainLable.bottom+10, explainLable.width-10, 12)];
//        
//        _moneyTitle.textColor = [UIColor whiteColor];
//        
//        _moneyTitle.font = [UIFont systemFontOfSize:11];
//        
//        [_backGroundButton addSubview:_moneyTitle];

        CGFloat left = explainLable.left+10;
        _conditionTiele = [[UILabel alloc] initWithFrame:CGRectMake(left, explainLable.bottom+10, _backGroundButton.width-left-20, 12)];
        
        
        
        _conditionTiele.textColor = [UIColor whiteColor];
        
        _conditionTiele.numberOfLines=0;
        
        _conditionTiele.font = [UIFont systemFontOfSize:11];
        
        [_backGroundButton  addSubview:_conditionTiele];
        
        _time = [[UILabel alloc] initWithFrame:CGRectMake(_conditionTiele.left, _conditionTiele.bottom+5, _conditionTiele.width, 12)];
        
        _time.numberOfLines=0;
        
        _time.textColor = [UIColor whiteColor];
        
        _time.font = [UIFont systemFontOfSize:11];
        
        [_backGroundButton addSubview:_time];


        
    }
    
    return  self;
}
-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    UIFont*font;
    
    if (SCREEN_WIDTH<375) {
        
        font=[UIFont systemFontOfSize:24];
        
    }else{
        
        font=[UIFont systemFontOfSize:36];
    }
    
   
    
    if ([_model.status intValue]==1) {
        
         [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"我的红包-未使用"] forState:0];
        
    }else if ([_model.status intValue]==2){
        
         [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"我的红包-已使用"] forState:0];
       
        
    }else if ([_model.status intValue]==3){
        
         [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"我的红包-已过期"] forState:0];
        
    }

    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = NSNumberFormatterRoundFloor;
    
    NSString*newApr = [formatter stringFromNumber:[NSNumber numberWithFloat:[_model.money floatValue]]];

    
    
    NSArray*arr=[[NSString stringWithFormat:@"￥%@",newApr] componentsSeparatedByString:@"."];
    
    NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@",newApr]];
    
    NSString*str=arr[0];
    
     [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(1, str.length-1)];
    
     _moneyLabel.attributedText=string;
    
    
//     _moneyTitle.text=@"托管红包";

    CGSize size=[_model.remark sizeWithFont:[UIFont systemFontOfSize:11] andMaxSize:CGSizeMake(_conditionTiele.width, 1000)];
    
     _conditionTiele.height=size.height;
    
     _conditionTiele.text=_model.remark;

    _time.top=_conditionTiele.bottom+5;
    
     _packetStyle.text=_model.name;
    _packetStyle.adjustsFontSizeToFitWidth = YES;
    if (_packetStyle.font.pointSize > 12) {
        _packetStyle.font = [UIFont systemFontOfSize:12];
    }
    
    
     _time.text=[NSString stringWithFormat:@"使用期限:%@",[NSDate getDateWithInterval:[_model.end_time doubleValue] formart:@"yyyy-MM-dd HH:mm:ss"]];
     CGSize timeSize=[_time.text sizeWithFont:[UIFont systemFontOfSize:11] andMaxSize:CGSizeMake(_time.width, 1000)];
    _time.height = timeSize.height;
}

-(NSMutableAttributedString*)getpointStr:(NSString*)str{
    
     NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",str]];
    
    [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(0, 1)];
    
    
    return string;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
